package com.zyl.pts.web.controller;

import com.zyl.pts.config.enums.CodeAndMessageEnum;
import com.zyl.pts.config.result.Result;
import com.zyl.pts.domain.bean.Account;
import com.zyl.pts.service.UserService;
import com.zyl.pts.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version v1.0
 * @ProjectName: product_source
 * @Package: com.zyl.pts.web.controller
 * @ClassName: AuthController
 * @Description TODO(认证模块)
 * @Author 翟玉龙
 * @Date 2024/11/19-9:12
 */
@Tag(name = "认证模块", description = "认证模块")
@RestController
public class AuthController {

    private final UserService userService;

    public AuthController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户登录接口
     * @param username 用户名
     * @param password 密码
     * @return {@link Result }<{@link ? }>
     */
    @Operation(summary = "登录", description = "用户登录")
    @PostMapping("/login")
    public Result<?> userLogin(
            String username,
            String password
    ) {
        return Result.success(
                CodeAndMessageEnum.SUCCESS,
                userService.login(username, password)
        );
    }

    /**
     * 退出登录接口
     * @return {@link Result }<{@link ? }>
     */
    @Operation(summary = "退出登录", description = "退出登录")
    @GetMapping("/logout")
    public Result<?> logout() {
        return Result.success(
                userService.logout()
        );
    }

    /**
     * 获取当前登录用户信息接口
     * @return {@link Result }<{@link ? }>
     */
    @Operation(summary = "获取当前登录用户信息", description = "获取当前登录用户信息")
    @GetMapping("/me")
    public Result<?> getUserInfo() {
        return Result.success(
                CodeAndMessageEnum.SUCCESS,
                (Account) ThreadLocalUtil.get()
        );
    }
}
